package com.bestcem.xm.txtanls.util;

import com.alibaba.fastjson.JSONArray;
import com.bestcem.xm.componet.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 行业关键词缓存类
 *
 * @author liheng
 * @version v1.0
 * @date 2021/5/11-16:25
 */

@Component
public class IndustryKeywordUtil {

    private static final String INDUSTRY_KEY = "txtanls:industry:keyword";

    @Autowired
    private RedisService redisService;

    /**
     * 缓存单个行业关键词
     *
     * @param industryCode 行业code
     * @param keywords     关键词list
     */
    public void cacheSingleIndustry(String industryCode, JSONArray keywords) {
        redisService.hashSet(INDUSTRY_KEY, industryCode, keywords);
    }

    /**
     * 根据code获取行业关键词list
     *
     * @param code 行业code(一般为Integer类型，这里需要转化为string)
     * @return
     */
    public JSONArray getKeywordByCode(String code) {
        Object o = redisService.hashGet(INDUSTRY_KEY, code);
        JSONArray strings = new JSONArray();
        if (Objects.nonNull(o)) {
            strings = JSONArray.parseArray(JSONArray.toJSONString(o));
        }
        return strings;
    }

    /**
     * 判断是否存在对应行业关键词缓存
     *
     * @param code
     * @return
     */
    public boolean existKey(String code) {
        return redisService.hashHasKey(INDUSTRY_KEY, code);
    }

    /**
     * 刷新缓存
     *
     * @param industryCode
     * @param keywords
     */
    public void refreshKey(String industryCode, JSONArray keywords) {
        redisService.hashDel(INDUSTRY_KEY, industryCode);
        redisService.hashSet(INDUSTRY_KEY, industryCode, keywords);
    }


}
